package com.fhz.kotlinmvvm.service

import android.R
import android.app.Notification
import android.app.NotificationManager
import android.app.PendingIntent
import android.app.Service
import android.content.Intent
import android.os.Binder
import android.os.IBinder
import android.util.Log
import android.widget.RemoteViews
import com.fhz.kotlinmvvm.MainActivity


/**
 *
 */
private const val TAG = "MyService"

class MyService : Service() {

    override fun onCreate() {
        super.onCreate()
        Log.i(TAG, "onCreate: ")
        //启动
        //发送一个前台通知,就可以变成前台服务.\

//        val  manager = getSystemService(NOTIFICATION_SERVICE) as NotificationManager
        val builder = Notification.Builder(this)
        builder.setSmallIcon(com.fhz.kotlinmvvm.R.mipmap.ic_launcher)

        //自定义广播中的布局
        val views = RemoteViews(packageName, com.fhz.kotlinmvvm.R.layout.notifi_layout)


        val intent = Intent(this,MainActivity::class.java)
        val activity =
            PendingIntent.getActivity(this, 888, intent, PendingIntent.FLAG_UPDATE_CURRENT)

        views.setOnClickPendingIntent(com.fhz.kotlinmvvm.R.id.notify_tv,activity)

        //TODO 从这里继续

        builder.setCustomContentView(views)


        startForeground(1,builder.build())
    }


    override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
        Log.i(TAG, "onStartCommand: ")
        return super.onStartCommand(intent, flags, startId)

    }

    inner class MyBinder : Binder(){
        fun callShow(){
            show()
        }
    }


    override fun onBind(intent: Intent): IBinder {
        Log.i(TAG, "onBind: ")
        return MyBinder()
    }

    override fun onUnbind(intent: Intent?): Boolean {
        Log.i(TAG, "onUnbind: ")
        return super.onUnbind(intent)
    }

    fun show(){
        println(111)
    }

    override fun onDestroy() {
        super.onDestroy()
        Log.i(TAG, "onDestroy: ")
    }
}